package com.jplus.service.impl;

import com.jplus.VO.ResultVO;
import com.jplus.dao.UserDao;
import com.jplus.dao.UserInfoDao;
import com.jplus.entity.UserInfo;
import com.jplus.exception.SFUmbrellaException;
import com.jplus.service.UserInfoService;
import com.jplus.utils.ResultVOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private UserDao userDao;

    @Override
    public ResultVO saveOneUserInfo(UserInfo userInfo) {
        // 先找到用户id
        try {
            userInfo.setUserId(Integer.valueOf(userDao.getUserIdByPhone(userInfo.getPhone())));
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("查找用户id出错");
            throw new SFUmbrellaException(0, "查找用户id出错");
        }
        try {
            userInfoDao.saveOneUserInfo(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("保存用户详细信息出错");
            throw new SFUmbrellaException(0, "保存用户详细信息出错");
        }
        return ResultVOUtils.success("保存用户详细信息成功");
    }
}
